import BuffUtil from '@/BuffUtil';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `每当造成${ColorSafe}1000|r点魔法伤害会进入焰魂状，提升${ColorSafe}120|r攻击速度和${ColorSafe}30%|r魔法伤害，持续${ColorSafe}10|r秒。触发间隔${ColorSafe}20|r秒。|n觉醒效果：触发临界值提升到${ColorSafe}2000|r点，但是移除触发间隔。`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNWallOfFire.blp';
const name = '焰魂';
const config: TalentConfigModel = {
    id: 'talent_t_TalentSoulOfFire',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentSoulOfFire',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentSoulOfFire_plus',
        art,
        name: '焰魂[觉醒]',
        ubertip: `每当造成${ColorSafe}2000|r点魔法伤害会进入焰魂状，提升${ColorSafe}120|r攻击速度和${ColorSafe}30%|r魔法伤害，持续${ColorSafe}10|r秒`,
    },
};

const effectBuffType: AppBuffType = {
    id: 'buff_t_TalentSoulOfFire_effect',
    dur: 10,
    name: '焰魂',
    ubertip: '当前处于焰魂状态，提升120%的攻击速度和30%的魔法伤害。',
    art,
    attribute: {
        attackSpd_p: 1.2,
        法术伤害倍率: 0.3,
    },
    targetAttach: 'foot',
    targetArt: 'Abilities\\Spells\\Orc\\LiquidFire\\Liquidfire.mdl',
};

const cooldownBuffType: AppBuffType = {
    id: 'buff_t_TalentSoulOfFire_cool',
    type: '基础',
    name: '焰魂冷却',
    dur: 20,
    ubertip: '当前处理焰魂的冷却时间内，无法再次激活',
    art,
};

export default class TalentSoulOfFire extends TalentBase {
    static instance: TalentSoulOfFire;
    damagePool: { [index: number]: number } = {};
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentSoulOfFire.instance = this;
        this.setTalentConfig(config);
        this.init();
        EventManager.onTakeDamage(this.damageEffect);
    }
    damageEffect(d: DamageEventData) {
        if (d.isPhysical) return;
        let _this = TalentSoulOfFire.instance;
        let { unEffect, plus } = _this.talentUnEffect(d.source);
        if (unEffect) return;
        if (BuffUtil.hasUnitBuff(d.source, effectBuffType.id) || BuffUtil.hasUnitBuff(d.source, cooldownBuffType.id)) return;
        let h = GetHandleId(d.source);
        let cur = (_this.damagePool[h] = (_this.damagePool[h] ?? 0) + d.lastDamage);
        let max = plus ? 2000 : 1000;
        if (cur >= max) {
            _this.damagePool[h] = 0;
            BuffUtil.addBuff(d.source, effectBuffType.id, d.source);
            DestroyEffect(AddSpecialEffect('Abilities\\Spells\\Human\\Avatar\\AvatarCaster.mdl', GetUnitX(d.source), GetUnitY(d.source)));
            if (!plus) BuffUtil.addBuff(d.source, cooldownBuffType.id, d.source);
        }
    }
    init() {
        BuffUtil.registerBuffType(effectBuffType);
        BuffUtil.registerBuffType(cooldownBuffType);
    }
}
